<template>
  <ul class="c-log">
    <li v-if="list.length===0">
      暂无数据
    </li>
    <template v-else>
      <li v-for="(l,i) in list" :key="i" class="operator">
        <div class="time">{{ l.createTime }}</div>
        <div class="content">
          <template v-for="(v,k) in kv">
            <div v-if="l[k]!==''&&l[k]!==undefined" :key="k"><span class="label">{{ getLabel(v) }}:</span><span>{{ formatData(v,k,l) }}</span></div>
          </template>
        </div>
      </li>
    </template>
  </ul>
</template>
<script>
import { postLogPage } from '@/api/common'
export default {
  name: 'CLog',
  props: {
    sourceId: {
      type: String,
      default: ''
    },
    keyValue: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      list: [],
      kv: {
        'createBy': '操作人',
        'step': '步骤',
        'result': { label: '结果', formatData: v => v ? '通过' : '不通过' },
        'advice': '意见',
        'context': '操作内容'
      }
    }
  },
  watch: {
    sourceId: {
      handler(v) {
        this.getLogData(v)
      },
      immediate: true
    },
    keyValue: {
      handler() {
        Object.entries(this.keyValue).forEach((k, v) => {
          this.$set(this.kv, k, v)
        })
      },
      immediate: true
    }
  },
  methods: {
    getLogData(sourceId) {
      const id = sourceId || this.$route.query.id
      if (!id) return
      postLogPage(id).then(res => {
        this.list = res.data.records.map(e => {
          try {
            const ctx = JSON.parse(e.context)
            Object.entries(ctx).forEach(([k, v]) => {
              e[k] = v
            })
          // eslint-disable-next-line no-empty
          } catch (error) {

          }
          return e
        }).sort((f, s) => {
          return new Date(s.createTime).getTime() - new Date(f.createTime).getTime()
        })
      })
    },
    getLabel(value) {
      return Object.prototype.toString.call(value) === '[object Object]' ? value.label : value
    },
    formatData(value, key, data) {
      if (Object.prototype.toString.call(value) === '[object Object]' && value.formatData) {
        return value.formatData(data[key])
      }
      return data[key]
    }
  }
}
</script>
<style lang="scss" scoped>
.c-log{
  list-style: none;
  padding: 0;
  .operator{
    position: relative;
    font-size: 12px;
    font-weight: 500;
    padding-left: 12px;
    .time{
      color: #333;
    }
    .content{
      color: #999;
      padding-left: 10px;
      .label{
        color: #333;
        font-weight: 500;
        padding-right: 10px;
      }
    }
    &::after{
      content: '';
      position: absolute;
      top: 6px;
      left: 0;
      display: block;
      width: 8px;
      height: 8px;
      border-radius: 50%;
      background-color: #09C595;
    }
    &::before{
      content: '';
      position: absolute;
      top: 6px;
      left: 3px;
      display: block;
      width: 1px;
      height: calc(100% + 12px);
      background-color: #F5F6F7;
      // background-color: red;
    }
  }
  .operator + .operator{
    margin-top: 12px;
  }
}
</style>
